<?php

include ("db_connect.php");
$db = new DB_CONNECT(); //ket noi den DB
$request = array(); //mang luu thong tin yeu cau nguoi dung
$response = array(); //array for JSON response

/**
 * Ham dung de phan tich yeu cau theo mang input
 * 
 **/
function check_array($input, array $array = array())
{
    foreach ($array as $arr)
    {
        $text = (string )$arr;
        if (strlen(strstr($input, $text)))
        {
            return 1;
            break;
        } else
        {
            return 0;
        }
    }
}
/**
 * Ham dung de chuan hoa xau dau vao 
 * 
 */
function chuanhoa($input)
{
    $input = strtolower($input); //chuyen cac ki tu in hoa thanh thuong
    $output = preg_replace('!\s+!', ' ', $input); //loai bo cac nhieu khoang trang giua cac tu
    $output = trim($output); //loai bo khoang trang o dau va cuoi chuoi
    return $output;
}

/**
 *  A function analysis of user requirements
 *  yeu cau ve category
 *  yeu cau ve gia ca
 *  yeu cau ve khu vuc
 * 
 */
function analyse($input)
{

    $input = chuanhoa($input); //chuan hoa xau dau vao
    $arr = split(" ", $input); //cat xau thanh cac tu rieng biet de xu ly

    /**
     * tim kiem yeu cau ve category
     * 
     **/
    // 1: nha hang, quan an

    if (in_array("an", $arr) || strstr($input, "quan an") || strstr($input, "an uong") || strstr($input,
        "doi") || strstr($input, "khat") || strstr($input, "nha hang") || strstr($input,
        "bun cha") || strstr($input, "pho") || strstr($input, "banh my xien nuong"))
    {
        $request['category'] = 1;
    }

    // 2: trung tam thuong mai

    if (in_array("mua", $arr) || strstr($input, "mua sam") || strstr($input,
        "mua ban") || strstr($input, "trung tam thuong mai"))
    {
        $request['category'] = 2;
    }

    // 3: khu vui choi giai tri
    //$arrInput = array("choi", "giai tri");
    //if (check_array($input, $arrInput) == 1) {
    //    $request['category'] = 3;
    //}
    if (in_array("choi", $arr) || strstr($input, "giai tri"))
    {
        $request['category'] = 3;
    }

    // 4: rap chieu phim
    if (in_array("phim", $arr) || in_array("movie", $arr) || strstr($input,
        "xem phim") || strstr($input, "chieu bong") || strstr($input, "rap phim"))
    {
        $request['category'] = 4;
    }

    // 5: dia diem du lich
    if (strpos($input, "du lich") || strstr($input, "canh dep") || strstr($input,
        "van hoa") || strstr($input, "tham quan"))
    {
        $request['category'] = 5;
    }

    /**
     * tim kiem yeu cau ve gia ca
     * 
     **/
    if (strstr($input, "re") || strstr($input, "gia re") || strstr($input,
        "gia thap") || strstr($input, "chi phi thap"))
    {
        $request['cost'] = 2;
    }

    if (strstr($input, "chi phi binh thuong") || strstr($input, "gia tot"))
    {
        $request['cost'] = 3;
    }

    if (strstr($input, "thuong luu") || strstr($input, "dai gia") || strstr($input,
        "chat luong cao") || strstr($input, "hao hang") || strstr($input, "thuong hang"))
    {
        $request['cost'] = 4;
    }
    /**
     * tim kiem yeu cau ve dia diem 
     * 
     **/
    if (strstr($input, "ba dinh"))
    {
        $request['location'] = 1;
    }

    if (strstr($input, "hoan kiem"))
    {
        $request['location'] = 2;
    }

    if (strstr($input, "tay ho"))
    {
        $request['location'] = 3;
    }

    if (strstr($input, "long bien"))
    {
        $request['location'] = 4;
    }

    if (strstr($input, "cay giay"))
    {
        $request['location'] = 5;
    }

    if (strstr($input, "dong da"))
    {
        $request['location'] = 6;
    }

    if (strstr($input, "hai ba trung"))
    {
        $request['location'] = 7;
    }

    if (strstr($input, "hoang mai"))
    {
        $request['location'] = 8;
    }

    if (strstr($input, "thanh xuan"))
    {
        $request['location'] = 9;
    }

    if (strstr($input, "ha dong"))
    {
        $request['location'] = 10;
    }

    if (strstr($input, "tu liem"))
    {
        $request['location'] = 11;
    }
    return $request;
}


/**
 * ham dung de tinh toan khoang cach giua 2 dia diem
 * 
 **/


/**
 * ham dung de truy van tu DB
 * 
 **/
function search(array $request = array())
{
    $query = "SELECT * FROM place ";
    if (isset($request['category']))
    {
        $category = $request['category'];
        $query = $query . "WHERE cat = {$category} ";
    }
    if (isset($request['cost']))
    {
        $cost = $request['cost'];
        $query = $query . "AND cost = {$cost} ";
    }
    if (isset($request['location']))
    {
        $location = $request['location'];
        $query = $query . "AND location = {$location}";
    }
    $result = mysql_query($query) or die(mysql_error());

    //check for empty result
    if (mysql_num_rows($result) > 0)
    {
        //looping through all result
        $response['place'] = array();

        while ($row = mysql_fetch_array($result))
        {
            //temp array
            $place = array();
            $place['lat'] = $row['lat'];
            $place['lang'] = $row['lang'];
            $place['name'] = $row['name_Place'];
            $place['countVote'] = $row['countVote'];
            $place['cat'] = $row['cat'];
            $place['cost'] = $row['cost'];
            $place['location'] = $row['location'];
            $place['diaChi'] = $row['diaChi'];
            $place['moTa'] = $row['moTa'];

            //push data into final response array
            array_push($response['place'], $place);
        }
        //success
        $response['success'] = 1;

        //echo JSON
        echo json_encode($response);
    } else
    {
        //no place found
        $response['success'] = 0;
        $response['message'] = "no place in DB";

        //echo JSON
        echo json_encode($response);
    }
}

$test = "toi muon an o hai ba trung";
//$array = array("choi", "giai tri");
//echo chuanhoa($test) . "<br/>";
//$test = chuanhoa($test);
//$out = analyse($test);
//echo $out['cost'];
//echo $out['category'];
//echo "<pre>";
//print_r($array);
//echo "</pre>";
//echo check_array($test, $array);
//$checkStr = "giai tri";
//echo strstr($input, $checkStr);
$output = analyse($test);
//echo "<pre>";
//print_r($output);
//echo "</pre>";
search($output);

?>